# -*- coding: utf-8 -*-
# @Time : 2024/2/5 10:17
# @Author : ZH
# @File : 1696.py
# @Software: PyCharm
class Solution(object):
    def maxResult(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        if len(nums) == 1:
            return nums[0]
        if k >= len(nums):
            return max([nums[0] + i for i in nums[0:]])

        index = 0
        max_num = nums[index]

        while True:
            start_pointer = index + 1
            max_index = start_pointer
            max_pointer = max_num + nums[start_pointer]
            move = k - 1
            for i in range(move):
                start_pointer += 1
                if start_pointer > len(nums) - 1:
                    return max_num if max_num > max_pointer else max_pointer
                if max_pointer < max_num + nums[start_pointer]:
                    max_index = start_pointer
            index = max_index
            max_num += nums[index]
            if not nums[index+1: index+k]:
                break
        return max_num


if __name__ == '__main__':
    # nums =[100,-1,-100,-1,100]
    nums = [1,-1,-2,4,-7,3]
    k = 2
    so = Solution()
    z = so.maxResult(nums=nums, k=k)
    print(z)

